package com.yb.guigu.pick.code.generator.listener;

import com.rabbitmq.client.Channel;
import com.yb.guigu.pick.code.generator.service.IMobileService;
import com.yb.guigu.pick.core.constant.EmailType;
import com.yb.guigu.pick.core.constant.MobileType;
import com.yb.guigu.pick.core.constant.RabbitMQConstant;
import com.yb.guigu.pick.core.domain.mq.EmailSend;
import com.yb.guigu.pick.core.domain.mq.MobileCodeSend;
import com.yb.guigu.pick.mail.sender.bean.MailSenderRequest;
import com.yb.guigu.pick.mail.sender.bean.MailSenderResponse;
import com.yb.guigu.pick.mail.sender.sender.IGuiguMailSender;
import com.yb.guigu.pick.mobile.sender.bean.MobileSenderRequest;
import com.yb.guigu.pick.mobile.sender.bean.MobileSenderResponse;
import com.yb.guigu.pick.mobile.sender.sender.IMobileSender;
import com.yb.guigu.pick.rabbitmq.messager.api.model.MessageContent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Component;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.IOException;

/**
 * Copyright (C), 2022-2023, 姚兵
 * Author: 32210
 * Date: 2023/6/11 21:00
 * FileName: EmailListener
 * Description:
 */

@Component
@Slf4j
public class MobileListener {


    @Autowired
    IMobileSender mobileSender;

    @Autowired
    IMobileService mobileService;

   @RabbitListener(queues = RabbitMQConstant.MobileQueueConstant.QUEUE)
    public void handle(@Payload MessageContent<MobileCodeSend> result, Message message, Channel channel) throws IOException {
        try {
            String body = new String(message.getBody());
            log.info("body:{}",body);


            MobileCodeSend mobileCodeSend = result.getContent();

            mobileService.sendMobileCode(mobileCodeSend);

            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }catch (Exception e){

            log.error("MobileListener:{}",e);

            channel.basicReject(message.getMessageProperties().getDeliveryTag(),false);
        }
    }


}
